import os

from generatorPlus import GeneratorPlus
from flask import request, render_template, Flask, send_from_directory, send_file, make_response
from flask_cors import CORS

app = Flask(__name__)
CORS(app, resources=r'/*')


@app.route('/')
def hello_world():
    return render_template('index.html')


@app.route('/generatorPlus',)
def generatorPlus():
    path = './'
    try:
        gen = GeneratorPlus(
            host=request.args.get('host'),
            port=int(request.args.get('port')),
            user=request.args.get('user'),
            password=request.args.get('password'),
            database=request.args.get('database'),
            table=request.args.get('table'),
        )
        gen.start(
            path=request.args.get('path')
        )
        return send_file(
            request.args.get('table') + os.path.basename(request.args.get('path'))
            , path
        )
    except Exception as e:
        response = make_response(str(e))
        response.status = 201
        return response


if __name__ == '__main__':
    # 这里用的是plus版本 更便捷
    app.run(port=8888)
